<?php
/*
  $Id: optional_related_products.php, ver 1.0 02/05/2007 Exp $

  Copyright (c) 2007 Anita Cross (http://www.callofthewildphoto.com/)

  Part of Contribution: Optional Related Products Ver 4.0

  Based on code from Optional Relate Products, ver 2.0 05/01/2005
  Copyright (c) 2004-2005 Daniel Bahna (daniel.bahna@gmail.com)

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Released under the GNU General Public License
 */

$orderBy = 'ORDER BY ';
$orderBy .= ( RELATED_PRODUCTS_RANDOMIZE) ? 'rand()' : 'pop_order_id, pop_id';
$orderBy .= ( RELATED_PRODUCTS_MAX_DISP) ? ' limit ' . RELATED_PRODUCTS_MAX_DISP : '';
$attributes = "
         SELECT
         pop_products_id_slave,
         products_name,
         products_model,
         products_price,
         products_quantity,
         products_tax_class_id,
         products_image
         FROM " .
        TABLE_PRODUCTS_RELATED_PRODUCTS . ", " .
        TABLE_PRODUCTS_DESCRIPTION . " pa, " .
        TABLE_PRODUCTS . " pb
         WHERE pop_products_id_slave = pa.products_id
         AND pa.products_id = pb.products_id
         AND language_id = '" . (int) $languages_id . "'
         AND pop_products_id_master = '" . $HTTP_GET_VARS['products_id'] . "'
         AND products_status='1' " . $orderBy;
$attribute_query = tep_db_query($attributes);

if (mysql_num_rows($attribute_query) > 0) {
    $count = 0;
    $first_related_product = true;
    while ($attributes_values = tep_db_fetch_array($attribute_query)) {
    	$count ++;
        $products_name_slave = ($attributes_values['products_name']);
        $products_model_slave = ($attributes_values['products_model']);
        $products_image_slave = ($attributes_values['products_image']);
        $products_qty_slave = ($attributes_values['products_quantity']);
        $products_id_slave = ($attributes_values['pop_products_id_slave']);
        $products_status_slave = 'undefined';
        if ($new_price = tep_get_products_special_price($products_id_slave)) {
            $products_price_slave = $currencies->display_price($new_price, tep_get_tax_rate($attributes_values['products_tax_class_id']));
        } else {
            $products_price_slave = $currencies->display_price($attributes_values['products_price'], tep_get_tax_rate($attributes_values['products_tax_class_id']));
        }
?>
        <div class="related-product<?php if ($first_related_product)
            echo ' first-related-product'; $first_related_product = false; ?>">
            <div class="related-product-general">
                <table>
                    <tr><td colspan="2" class="product-name"><a href="<?php echo tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products_id_slave); ?>"><b><?php echo $products_name_slave; ?></b></td></tr>
                    <tr><td>Giá</td><td><?php echo $products_price_slave; ?></td></td></tr>
                    <tr><td>Tình trạng:</td><td><?php echo $products_status; ?></td></td></tr>
                </table>
            </div>
            <div class="related-product-image">
                <a href="<?php echo tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' .$products_id_slave); ?>"><?php echo tep_image(DIR_WS_IMAGES . $products_image_slave, $products_name_slave, 80, 104) ?></a>
            </div>
        </div>
        <?php if ($count%3==0){
        	$first_related_product = true;
        	?>        
        	</div>
        	<div id="content-footer">
      <?php }
    }
}
?>


